package custcare

import (
	"strconv"
	"wave-admin/global"
	"wave-admin/model/common/response"
	"wave-admin/model/custcare/request"
	"wave-admin/utils"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type ReplyApi struct{}

// @Tags Reply
// @Summary 添加回复
// @Produce application/json
// @Param Authorization header string true "验证参数Bearer和token空格拼接"
// @Param data body request.ReqReply true "回复内容,状态,排序"
// @Success 200 {string} string "{"code":200,"data":null,"msg":"添加自动回复成功！"}"
// @Router /api/v1/reply/reply [post]
func (a *ReplyApi) AddReply(ctx *gin.Context) {
	reply := &request.ReqReply{}
	if err := ctx.ShouldBindJSON(reply); err != nil {
		response.FailWithMessage(ctx, utils.Error(err))
		return
	}
	if err := replyService.AddReply(*reply); err != nil {
		global.GnLog.Error("添加自动回复失败!", zap.Error(err))
		response.FailWithMessage(ctx, err.Error())
	} else {
		response.SuccessWithMessage(ctx, "添加自动回复成功！")
	}
}

// @Tags Reply
// @Summary 编辑回复
// @Produce application/json
// @Param Authorization header string true "验证参数Bearer和token空格拼接"
// @Param data body request.ReqReply true "回复内容,状态,排序"
// @Success 200 {string} string "{"code":200,"data":null,"msg":"编辑自动回复成功！"}"
// @Router /api/v1/reply/reply/:id [put]
func (a *ReplyApi) UpdateReply(ctx *gin.Context) {
	id, _ := strconv.Atoi(ctx.Param("id"))
	reply := &request.ReqReply{}
	if err := ctx.ShouldBindJSON(reply); err != nil {
		response.FailWithMessage(ctx, utils.Error(err))
		return
	}
	if err := replyService.UpdateReply(*reply, id); err != nil {
		global.GnLog.Error("编辑自动回复失败!", zap.Error(err))
		response.FailWithMessage(ctx, err.Error())
	} else {
		response.SuccessWithMessage(ctx, "编辑自动回复成功！")
	}
}

// @Tags Reply
// @Summary 删除回复
// @Produce application/json
// @Param Authorization header string true "验证参数Bearer和token空格拼接"
// @Success 200 {string} string "{"code":200,"data":null,"msg":"删除自动回复成功！"}"
// @Router /api/v1/reply/reply/:id [delete]
func (a *ReplyApi) DeleteReply(ctx *gin.Context) {
	id, _ := strconv.Atoi(ctx.Param("id"))
	if err := replyService.DeleteReply(id); err != nil {
		response.FailWithMessage(ctx, err.Error())
	} else {
		global.GnLog.Error("删除自动回复失败!", zap.Error(err))
		response.SuccessWithMessage(ctx, "删除自动回复成功！")
	}
}

// @Tags Reply
// @Summary 自动回复列表
// @Produce application/json
// @Param Authorization header string true "验证参数Bearer和token空格拼接"
// @Param data body request.ReplyList true "回复内容,状态"
// @Success 200 {string} string "{"code":200,"data":{"list":[],"total":7,"current_page":1,"page_size":20},"msg":"获取自动回复列表成功！"}"
// @Router /api/v1/reply/getList [get]
func (a *ReplyApi) ReplyList(ctx *gin.Context) {
	pageInfo := &request.ReplyList{}
	if err := ctx.ShouldBindQuery(pageInfo); err != nil {
		response.FailWithMessage(ctx, utils.Error(err))
		return
	}
	if list, total, err := replyService.GetReplyList(*pageInfo); err != nil {
		global.GnLog.Error("获取自动回复列表失败!", zap.Error(err))
		response.FailWithMessage(ctx, "获取自动回复列表失败"+err.Error())
	} else {
		response.Success(ctx, response.PageResult{
			List:        list,
			Total:       total,
			CurrentPage: pageInfo.CurrentPage,
			PageSize:    pageInfo.PageSize,
		}, "获取自动回复列表成功！")
	}
}

// @Tags Reply
// @Summary 自动回复sort最大值
// @Produce application/json
// @Param Authorization header string true "验证参数Bearer和token空格拼接"
// @Success 200 {string} string "{"code":200,"data":{},"msg":"获得自动回复sort最大值成功！"}"
// @Router /api/v1/reply/getSort [get]
func (a *ReplyApi) GetSort(ctx *gin.Context) {
	if tag, err := replyService.GetSort(); err != nil {
		global.GnLog.Error("获取自动回复sort最大值失败!", zap.Error(err))
		response.FailWithMessage(ctx, err.Error())
	} else {
		response.Success(ctx, tag, "获得自动回复sort最大值成功！")
	}
}

// @Tags Reply
// @Summary 获取回复分组列表
// @Produce application/json
// @Param Authorization header string true "验证参数Bearer和token空格拼接"
// @Success 200 {string} string "{"code":200,"data":{},"msg":"获得回复分组列表成功！"}"
// @Router /api/v1/reply/getSelectReplyGroupList [get]
func (a *ReplyApi) SelectReplyGroupList(ctx *gin.Context) {
	if list, err := replyService.SelectReplyGroupList(); err != nil {
		global.GnLog.Error("获取回复分组列表失败!", zap.Error(err))
		response.FailWithMessage(ctx, err.Error())
	} else {
		response.Success(ctx, list, "获得回复分组列表成功！")
	}
}
